<html>
<head>
<title> <? echo XML_TITLE;?> </title>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_TITLE_GERA; ?></font>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_TITLE_MATRIZ; ?></font></p>
<p class=titulo align="center"><font face="Arial Black"><? echo XML_GERA_MATRIZ_SUBTITLE_2; ?></font></p>
<p>
<?
	include_once("config.php");
	require_once("verifica.php");
?>
</head>

<body>

<? if (strlen(@$arquivo)==0) { ?>

		<br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG01; ?></font>
		<br>
		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_determinante_xml.php" method="post">
		<input type="radio" name="arquivo" value="nao" unchecked><font face="Arial"><? echo XML_GERA_MSG02; ?></font><br>
		<input type="radio" name="arquivo" value="sim" unchecked><font face="Arial"><? echo XML_GERA_MSG03; ?></font><p>
		<input class="button" type="submit" name="Submit" value=<? echo XML_NEXT; ?>>
		</form>

<? } else if ((@$arquivo=="sim")&((strlen(@$text_file)==0)||(!file_exists(@$text_file)))&(@$file_read!="ok")) { ?>

		<br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG04; ?></font>
		<br>
		<form enctype=multipart/form-data action="index.php?inc=gera_prob_matriz_determinante_xml.php" method="post">
		<a href=javascript:winopen('ajuda/ajdados1.php?idioma=PT_BR.php')><font face="Arial"><? echo XML_GERA_MSG05; ?></a><p>
		<input type="file" name="text_file"><p>
		<? if ((strlen($text_file)!=0)&(!file_exists($text_file))) { ?>
		<font face="Arial"><? echo XML_GERA_MSG06; ?></font>
        <? } ?>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo XML_NEXT; ?>>
		</form>

<? } else if (((strlen(@$dim)==0) | (@$dim<1) | (@$dim>10))& ($arquivo=="nao"))  { ?>

		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_determinante_xml.php" method="post" onSubmit="return tamanhomax(this);">
		<p align="left"><font face="Arial"><? echo XML_GERA_MATRIZ_MSG01; ?></font>
		<b></b>
		<input type="text" size='2' maxlength='2' name="dim">
		<br><br>
		<input type="hidden" name="arquivo" value="<? echo $arquivo ?>">
		<input class="button" type="submit" name="Submit" value=<? echo XML_NEXT; ?>>
		</form>

<? } else if  (strlen(@$desc)==0 || !checaNumerico($A)) { ?>

		<form enctype="multipart/form-data" action="index.php?inc=gera_prob_matriz_determinante_xml.php" method="post">

        <? if ((@$arquivo=="sim")&(@$file_read!="ok")) {
        $fp = fopen($text_file, "r");
		$vet_dados = file($text_file);
		$dim = trim($vet_dados[0]);
		$cont = 0;
		for ($i=0; $i<$dim; $i++){
		   for ($j=0; $j<$dim; $j++){
			 $cont = $cont + 1;
			 $A[$i][$j]=trim($vet_dados[$cont]);
		   }
		}
		fclose($fp);
		$file_read = ok;
		}
		if (isset($A))
		{
			echo "<b><font color=\"#ff0000\">Os valores para a matriz devem ser num&eacute;ricos</font></b>";
		}
		?>
		<p align="left"><font face="Arial"><? echo XML_GERA_MATRIZ_MSG01; ?> <? echo @$dim ?></font>
		<br><br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MATRIZ_MSG02; ?></font>
		<br><br>
		<? for ($i=0; $i<$dim; $i++) {
			for ($j=0; $j<$dim; $j++) {
	                $aux=$j+1;  ?>
			    <input type="text" name="A[<? echo $i ?>][<? echo $j ?>]" value="<? echo @$A[$i][$j] ?>" maxlength="10" size="5">
   		<? } //end for j ?>
		<br>
		<? } //end for i ?>
		<br>
		<p align="left"><font face="Arial">Descri&ccedil;&atilde;o da Matriz :</font>
		<b></b>
		<input type="text" size='50' maxlength='50' name="desc" value="<? echo @$desc ?>">
		<br><br>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG08; ?></font><br>
		<TEXTAREA name="com" cols="50" rows="3"><? echo @$com ?></TEXTAREA>
		<p>
		<p align="left"><font face="Arial"><? echo XML_GERA_MSG010; ?></font><br>
		<TEXTAREA name="ref" cols="50" rows="3"><? echo @$ref ?></TEXTAREA>
		<p>
		<input type="hidden" name="arquivo" value="<? echo @$arquivo ?>">
	    <input type="hidden" name="file_read" value="<? echo @$file_read ?>">
		<input type="hidden" name=dim value="<? echo @$dim ?>">
		<input class="button" type="submit" name="Submit" value=<? echo XML_GERA_MSG010; ?>>
		</form>

<? } else {

$id_demo=0;
//$usr_file = $DOCUMENT_ROOT."/user/".$id_usuario."/biblioteca.xml";
$usr_file=$_SESSION['ANAWEB']->RetornaArquivoBiblioteca();
$dem_file = $DOCUMENT_ROOT."/user/".$id_demo."/biblioteca.xml";
function xmlentities($xml)
{
	$entities = array ('<' => '&lt;','&' => '&amp;',);
	if (is_string($xml)) { $xml = strtr($xml, $entities);};
	return $xml;
}
function gera_bib($xml_file,$desc,$dim,$A,$com,$ref)
{
		if (file_exists($xml_file))
        {
            $obj = new DOMDocument();
            $obj->preserveWhiteSpace=false;
            $obj->load($xml_file);
        }
		else
            $obj = new DOMDocument('1.0','iso8859-1');
        $obj->formatOutput = true;
        //criando a raiz "<problems>"
        if ($obj->getelementsbytagname("problems")->length == 0)
        {
            $raiz = $obj->createElement("problems");
            $raiz = $obj->appendChild($raiz);
        }
        else $raiz = $obj->getelementsbytagname("problems")->item(0);
        //elemento "<matrices>"
        if ($obj->getelementsbytagname("matrices")->length == 0)
        {
            $filho = $obj->createElement("matrices");
            $filho = $raiz->appendChild($filho);
        }
        else $filho = $obj->getelementsbytagname("matrices")->item(0);

        //criando novo elemento "<matrix>"
        $matrix = $obj->createElement("matrix");
		$matrix = $filho->appendChild($matrix);

        //descricao
        $description = $obj->createElement("description",xmlentities(utf8_encode($desc)));
        $description = $matrix->appendChild($description);
        //dimensao
        $dimension = $obj->createElement("dim",$dim);
        $dimension = $matrix->appendChild($dimension);

        //matriz em sim
		for($i = 0; $i < $dim; $i++)
		{
            $row = $obj->createElement("row");
            $row = $matrix->appendChild($row);
			for($j = 0; $j < $dim; $j++)
			{
                $x = $obj->createElement("x",$A[$i][$j]);
                $x = $row->appendChild($x);
			}
		}
        //comentario
		if (strlen($com)) {
            $comen = $obj->createElement("comments",xmlentities(utf8_encode($com)));
            $comen = $matrix->appendChild($comen);
		}
        //referencia
		if (strlen($ref)) {
            $refer = $obj->createElement("reference",xmlentities(utf8_encode($ref)));
            $refer = $matrix->appendChild($refer);
		}
        $obj->save($xml_file);
}
gera_bib($usr_file,$desc,$dim,$A,$com,$ref);
//gera_bib($dem_file,$desc,$dim,$A,$com,$ref);
?>
		<p align="center"><font face="Arial"><? echo XML_GERA_MSG011; ?></font></p>
        <?


} //end else

?>

		<form name="back" method="post" action="index.php?inc=gera_prob_matriz_determinante_xml.php">
		<input class="button" type="submit" name="Submit" value="<? echo XML_BEGIN; ?>">
		</form>

</body>
</html>
